package com.wang.test.demo.putong;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wangzhenjun
 * @date 2022/2/22 13:40
 */
public class JUC {


    static Object lock = new Object();
    static ReentrantLock reentrantLock = new ReentrantLock();
    static Condition condition = reentrantLock.newCondition();

    public static void main(String[] args) {
        // waitNotify();
        // awaitSignal();

        // lockSupport();
        DateTimeFormatter newFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate date2 = LocalDate.now();
        String format = date2.format(newFormatter);
        System.out.println();
        System.out.println(LocalDate.now());
    }

    private static void lockSupport() {
        Thread a = new Thread(()->{
			try {
				TimeUnit.SECONDS.sleep(3);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
            System.out.println(Thread.currentThread().getName() + "进来" + System.currentTimeMillis());
            LockSupport.park();
            System.out.println("只能走到这里了，下面走不通了没用许可证了");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName() + " 被换醒了" + System.currentTimeMillis());
        }, "A");
        a.start();
        Thread b = new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            LockSupport.unpark(a);
            LockSupport.unpark(a);
            System.out.println(Thread.currentThread().getName()+"唤醒传入的线程");
        }, "B");
        b.start();
    }

    private static void awaitSignal() {
        new Thread(()->{
            reentrantLock.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"进来");

                condition.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                reentrantLock.unlock();
            }

            System.out.println(Thread.currentThread().getName()+"我被唤醒了");
        },"A").start();

        new Thread(()->{
            reentrantLock.lock();
            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName()+"随机唤醒一个线程");
            }finally {
                reentrantLock.unlock();
            }
        },"B").start();
    }

    private static void waitNotify() {
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName()+"进来");
                try {
                    // 释放锁，陷入阻塞，直到有人唤醒
                    lock.wait();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName()+"我被唤醒了");
        }, "A").start();

        new Thread(()->{
            synchronized (lock) {
                lock.notify();
                System.out.println(Thread.currentThread().getName()+"随机唤醒一个线程");
            }
        }, "B").start();
    }
}
